$(function () {

    var html_data = {
        baseConfig: baseConfig,
        un_cash_order_index: 1,
        un_cash_order_count: 0,
        un_pay_order_list: [],
        pay_order_index: 1,
        pay_order_count: 0,
        order_list: [],
        sessionid: '',
        sessionid_q: '',
        unapply_price: 0,
        apply_price: 0,
        address_info: {},
        cash_list: [],
        has_login: false,
        alipay: "",
        money: 0,
        order_num: 0,
        order_price: 0,
        ping_money: 0,
        cash_money: 0,
        cash_order_num: 0,
        cash_order_price: 0,
        cash_ping_money: 0,
        un_money: 0,
        start_time:null,
        end_time:null


    };
    var app = new Vue({
        el: '#host_cash_page',
        data: html_data,
        methods: {
            price_filter: price_filter,
            query_withdraw_cash_apply: function () {
                api.lorder.query_withdraw_cash_apply().then(function (data) {
                    html_data.unapply_price = data.result.money;
                    html_data.apply_price = data.result.finish_money;
                    html_data.cash_list = data.result.list;

                });
            },
            query_withdraw_cash_info_by_date: function () {
                api.lorder.query_withdraw_cash_info_by_date().then(function (data) {
                    html_data.money = data.result.money;
                    html_data.order_num = data.result.order_num;
                    html_data.order_price = data.result.order_price;
                    html_data.ping_money = data.result.ping_money;

                    html_data.cash_money = data.result.cash_money;
                    html_data.cash_order_num = data.result.cash_order_num;
                    html_data.cash_order_price = data.result.cash_order_price;
                    html_data.cash_ping_money = data.result.cash_ping_money;
                    html_data.un_money = data.result.un_money;

                });
            },
            query_un_cash_order_info_list: function () {
                api.lorder.query_user_order_list({
                    is_withdraw_cash: false,
                    page_index: html_data.un_cash_order_index
                }).then(function (data) {
                    html_data.un_cash_order_index = data.result.page_index;
                    html_data.un_cash_order_count = data.result.page_count;
                    if (html_data.un_cash_order_index != data.result.page_count) {
                        html_data.un_cash_order_index += 1;
                    }
                    _(data.result.list).each(function (item) {
                        html_data.order_list.push(item);
                        item.goods_num = 0;
                        _(item.goods_order_item_list).each(function (obj) {
                            item.goods_num += obj.num
                        })
                    });
                });
            },
            query_pay_order_info_list: function () {
                api.lorder.query_pay_order_info_list({
                    charge_status: '2',
                    page_index: html_data.pay_order_index
                }).then(function (data) {
                    html_data.pay_order_index = data.result.page_index;
                    html_data.pay_order_count = data.result.page_count;
                    if (html_data.pay_order_index != data.result.page_count) {
                        html_data.pay_order_index += 1;
                    }
                    _(data.result.list).each(function (item) {
                        html_data.pay_order_list.push(item);
                    });
                });
            },

            create_cash: function () {
                if(html_data.unapply_price>0){
                    Magiclamp.mcConfirm("提现申请", "申请提现" + price_filter(html_data.unapply_price) + "元? <br> 是否确定提现到微信零钱包？", null, function () {
                        api.lorder.create_withdraw_cash_apply().then(function (data) {
                            Magiclamp.mcAlert("提示", data.message);
                            app.query_withdraw_cash_apply();
                            app.query_un_cash_order_info_list();
                        });
                    })
                }else{
                    Magiclamp.mcAlert("提示", "没有可提现的额度。")
                }

            },
            refrash_withdraw_cash_apply: function (obj) {
                api.lorder.refrash_withdraw_cash_apply({apply_id: obj.id}).then(function (data) {
                    obj.status = data.result.status;
                    obj.pay_status = data.result.pay_status;
                })
            },
            open_order_info: function (order_id) {
                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            open_order_list: function () {
                location.href = 'host_cash_list.html';
            },
            check_login: function () {
                var defer = $.Deferred();
                api.sys.check_login().then(function (data) {
                    if (data.result.has_login) {
                        client_storage('sessionid', data.result.sessionid);
                        defer.resolve({success: true});
                    } else {
                        Magiclamp.mcLoginPrompt(function (username, password) {
                            api.sys.simple_login({
                                is_live: true,
                                username: username,
                                password: password
                            }).then(function (login_result) {
                                if (login_result.success) {
                                    client_storage('sessionid', login_result.result.sessionid);
                                    defer.resolve({success: true});
                                } else {
                                    defer.reject({success: false});
                                }
                            }, function (data) {
                                defer.reject({success: false});
                            })
                        });
                    }
                }, function () {
                    defer.reject({success: false});
                });
                return defer.promise();
            },
            login_mogu: function () {
                html_data.end_time = moment(new Date()).format("YYYY-MM-DD");
                html_data.start_time = moment().subtract(31, 'days').format("YYYY-MM-DD");
                app.check_login().then(function (data) {
                    if (data.success) {
                        html_data.has_login = true;
                        app.query_withdraw_cash_apply();
                        // app.query_un_cash_order_info_list();
                        app.query_withdraw_cash_info_by_date();
                        //
                        // api.sys.get_host_alipay().then(function (data) {
                        //     html_data.user_name = data.result.user_name;
                        //     if (data.result.user_name) {
                        //         app.query_withdraw_cash_apply();
                        //         // app.query_un_cash_order_info_list();
                        //         app.query_withdraw_cash_info_by_date();
                        //     } else {
                        //         location.href = 'host_alipy.html';
                        //     }
                        // });
                    } else {

                        //app.login_mogu();
                    }
                }, function (data) {
                    //app.login_mogu();
                });
            },
            page_back: function () {
                baseConfig.page_back();
            }

        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }

    app.login_mogu();
    // app.query_withdraw_cash_apply();
    // app.query_un_cash_order_info_list();
    //
    //
    var $tags = $('#order-tab').find('a');
    window.mySwipe = new Swipe(document.getElementById('slider'), {
        continuous: false,
        disableScroll: false,
        stopPropagation: false,
        callback: function (index) {
            $tags.eq(index).addClass('tag-on')
                .siblings('a').removeClass('tag-on');
        },
        transitionEnd: function (index, elem) {
        }
    });
    $tags.eq(0).click(function (e) {
        mySwipe.prev();
        e.preventDefault();
    });
    $tags.eq(1).click(function (e) {
        mySwipe.next();
        e.preventDefault();
    });
    //
    // /*动态更新订单状态*/
    //
    // var time_allow=5;//查询时间间隔 单位秒
    // var max_num=40;//查询多大次数
    // change_dn_type_send();
    // function change_dn_type_send(){
    //     var isgoing=false; //是否继续查询
    //     if(max_num==0){//次数够了不在查询
    //         return false;
    //     }
    //     Magiclamp.mcAjax('/account/member/getDnOrderTypeSendStatusByuserid?'+max_num,'POST','',function (data) {
    //         max_num--;
    //         var msg='';            
    //         if(data.status==0){//没有查询到数据 查询结束
    //             return false;
    //         }
    //         jQuery.each(data.data,function (k,v) {
    //             if(v==2){
    //                 $('#dn-other-info-'+k).prev('.dn-msg-before').remove();
    //                 msg='已派送';
    //             }else if(v==1){
    //                 $('#dn-other-info-'+k).prev('.dn-msg-before').remove();
    //                 msg='送货中';
    //             }else{
    //                 isgoing=true;///继续查询
    //                 msg='正在准备商品，请耐心等待' ;
    //             }
    //             $('#dn-other-info-'+k).find('span').eq(0).html(msg);
    //         });
    //         if(isgoing){
    //             setTimeout(change_dn_type_send,time_allow*1000);
    //         }
    //     })
    // }
    //
    // $('.get-more').click(function () {
    //     var pageNum = $('#page_num').val();
    //     console.log(pageNum);
    //     var url = "/account/member/user_order/" + (parseInt(pageNum) + 1);
    //     var pdata = {};
    //     var _this = $(this);
    //     Magiclamp.listLoad();
    //     Magiclamp.mcAjax(url, "post", pdata, function (dataBack) {
    //             var html = '';
    //             if (dataBack.data.orders) {
    //                 dataBack.data.orders.forEach(function (v) {
    //                     html += '<div class="order-item g-wrap">';
    //                     html += '<div class="order-title">';
    //                     html += '<h1>'+v.com_title+'</h1>';
    //                     html += '<p>'+v.stime+'</p>';
    //                     html += '</div>';
    //                     var mygoods = JSON.parse(v.mygoods);
    //                     mygoods.forEach(function(g){
    //                                 html += '<div class="g-item">';
    //                                 html += '<div class="g-item-wrap">';
    //                                 html += '<a href="#"  class="g-img">';
    //                                 html += '<img src="'+g.preimg+'">';
    //                                 html += '</a>';
    //                                 html += '<div class="g-i-info">';
    //                                 html += '<p class="g-i-name">'+g.title+'</p>';
    //                                 html += '<p class="g-i-count">';
    //                                 html += '<span class="g-i-sum">';
    //                                 html += '<span>￥</span><span class="price">'+(g.price/100).toFixed(2)+'</span>';
    //                                 html += '</span>';
    //                                 html += '<span class="reg-count tar">';
    //                                 html += 'x';
    //                                 html += '<i class="g-sum">'+g.counts+'</i>';
    //                                 html += '</span>';
    //                                 html += '</p>';
    //                                 html += '<p class="g-i-mark">';
    //                                 switch (g.order_type){
    //                                     case '0':
    //                                         html += '<i class="goods-mark mark-sd">售卖机自取</i>';
    //                                         break;
    //                                     case '1':
    //                                         html += '<i class="goods-mark mark-dn">前台送货</i>';
    //                                         break;
    //                                 }
    //                                 html += '</p>';
    //                                 html += '</div>';
    //                                 html += '</div>';
    //                                 html += '</div>';
    //                     })
    //                     html += '<div class="order-info">';
    //                     html += '<span class="order-room">';
    //                     if(1 == v.order_type && 'undefined' != v.door_num){
    //                         html += '房间号: <span>'+v.door_num+'</span>';
    //                         html += '<?php endif;?>';
    //
    //                     }
    //                      html += '</span>';
    //                      html += '<div class="order-price">';
    //                      html += '订单合计:';
    //                      html += '<span>￥</span><span class="price">'+(v.order_allprice/100).toFixed(2)+'</span>';
    //                      html += '</div></div></div>';
    //                 })
    //             }
    //             $('#get_more_finish').before(html);
    //             $('#page_num').val(dataBack.data.page);
    //             Magiclamp.listLoadGet($('#get_more_finish'), '加载更多', 'auto');
    //             if (parseInt(pageNum) == $('#page_num').val() || dataBack.data.orders.length != $('#page_limit').val()) {
    //                 Magiclamp.listLoadGet($('#get_more_finish'), '已加载全部', 'none');
    //             }
    //
    //         var html = '';
    //         if (dataBack.data.unfinish) {
    //             dataBack.data.unfinish.forEach(function (v) {
    //                 html += '<div class="order-item g-wrap">';
    //                 html += '<div class="order-title">';
    //                 html += '<h1>'+v.com_title+'</h1>';
    //                 html += '<p>'+v.stime+'</p>';
    //                 html += '</div>';
    //                 var mygoods = JSON.parse(v.mygoods);
    //                 mygoods.forEach(function(g){
    //                     html += '<div class="g-item">';
    //                     html += '<div class="g-item-wrap">';
    //                     html += '<a href="#"  class="g-img">';
    //                     html += '<img src="'+g.preimg+'">';
    //                     html += '</a>';
    //                     html += '<div class="g-i-info">';
    //                     html += '<p class="g-i-name">'+g.title+'</p>';
    //                     html += '<p class="g-i-count">';
    //                     html += '<span class="g-i-sum">';
    //                     html += '<span>￥</span><span class="price">'+(g.price/100).toFixed(2)+'</span>';
    //                     html += '</span>';
    //                     html += '<span class="reg-count tar">';
    //                     html += 'x';
    //                     html += '<i class="g-sum">'+g.counts+'</i>';
    //                     html += '</span>';
    //                     html += '</p>';
    //                     html += '<p class="g-i-mark">';
    //                     switch (g.order_type){
    //                         case '0':
    //                             html += '<i class="goods-mark mark-sd">售卖机自取</i>';
    //                             break;
    //                         case '1':
    //                             html += '<i class="goods-mark mark-dn">前台送货</i>';
    //                             break;
    //                     }
    //                     html += '</p>';
    //                     html += '</div>';
    //                     html += '</div>';
    //                     html += '</div>';
    //                 })
    //                 html += '<div class="order-info">';
    //                 html += '<span class="order-room">';
    //                 if(1 == v.order_type && 'undefined' != v.door_num){
    //                     html += '房间号: <span>'+v.door_num+'</span>';
    //                     html += '<?php endif;?>';
    //                     html += '</span>';
    //                 }
    //                 html += '<div class="order-price">';
    //                 html += '订单合计:';
    //                 html += '<span>￥</span><span class="price">'+(v.order_allprice/100).toFixed(2)+'</span>';
    //                 html += '</div></div>';
    //                 html += '<div class="order-info dn-info">';
    //                 html += '<div class="dn-msg-before" style="float:left;" ><span class="waiting"></span></div>';
    //                
    //                 html += '   <span class="dn-other-info" id="dn-other-info-'+v.order_id+'">';
    //                 if(v.type_send == 1){
    //                     html += '<span class="dn-order-state">送货中</span>';
    //                 }else if(v.type_send == 2) {
    //                     html += '<span class="dn-order-state">已派送</span>';                        
    //                 }else{
    //                     html += '<span class="dn-order-state">正在准备商品，请耐心等待</span>';
    //                 }
    //                 html += '</span>';
    //                 html += '</div>';
    //                 html += '</div>';
    //             })
    //         }
    //         $('#get_more_unfinish').before(html);
    //         $('#page_num').val(dataBack.data.page);
    //         Magiclamp.listLoadGet($('#get_more_unfinish'), '加载更多', 'auto');
    //         if (parseInt(pageNum) == $('#page_num').val() || dataBack.data.unfinish.length != $('#page_limit').val() ) {
    //             Magiclamp.listLoadGet($('#get_more_unfinish'), '已加载全部', 'none');
    //         }
    //             Magiclamp.imgSrc();
    //         }, function (databack) {
    //             Magiclamp.listLoadGet(_this, databack.message, 'none');
    //         }, true
    //     );
    //     return false;
    // });
    //
    //
    // $('#r_btn').on('click',function () {
    //     url=$(this).data('href');
    //     Magiclamp.mcAjax(url,'get','',function (data) {
    //         console.log(data);
    //         if(data.status==1){
    //             // location.reload();
    //         }else{                
    //             // location.reload();
    //         }
    //     },function () {
    //        
    //     })
    // })
});